﻿using System;
using System.Collections.Generic;

using ApprovalTests;
using ApprovalTests.Reporters;

using FluentAssertions;

using JsonFx.Json;
using JsonFx.Serialization;

using NewModel.Accounting.Calculation;
using NewModel.Accounting.Core;
using NewModel.Accounting.Persistence;

using NUnit.Framework;

namespace NewModel.UnitTests.Accounting.Persistence
{
    [TestFixture]
    [UseReporter(typeof(VisualStudioReporter))]
    public sealed class TestLedgerPersistence
    {
        static readonly Ledger sLedger = new Ledger
            {
                Accounts = new List<Account>
                    {
                        Account.Root, 
                        Account.Asset, 
                        Account.Cache,
                        Account.Equity,
                        Account.Exchange,
                        Account.Expense,
                    },
                Currencies = new List<Currency>
                    {
                        Currency.Byr,
                        Currency.Eur,
                        Currency.Rur,
                        Currency.Usd
                    },
                Batches = new List<BatchDescriptor>
                {
                    new BatchDescriptor
                    {
                        ReferenceInterval = new Interval(new DateTime(2000, 1, 1), new DateTime(2001, 12, 31)),
                    }
                }
        };
        static readonly LedgerData sLedgerData = new LedgerData(sLedger);

        static readonly IDataWriter sWriter = new JsonWriter(
            new DataWriterSettings
            {
                PrettyPrint = true,
            });

        static readonly IDataReader sReader = new JsonReader();

        LedgerPersistence mUnderTest;

        [SetUp]
        public void SetUp()
        {
            mUnderTest = new LedgerPersistence();
        }

        [Test]
        public void JsonSerialization()
        {
            Approvals.Verify(sWriter.Write(sLedgerData));
        }
        [Test]
        public void LedgerData_Should_Deserialize_Fine()
        {
            sReader.Read<LedgerData>(sWriter.Write(sLedgerData))
                .ShouldBeEquivalentTo(sLedgerData);
        }
        [Test]
        public void LedgerData_Should_Convert_Nicely_Back_To_Ledger()
        {
            sLedgerData.Back(new Context()).ShouldBeEquivalentTo(sLedger);
        }
    }
}